import 'package:fl_chart/fl_chart.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:sleep/db/merroy_db.dart';
import 'package:sleep/model/setting/setting.dart';
import 'package:sleep/provider/db_provider.dart';
import 'package:sleep/provider/wave_deatil_provider/fatigue_detail_provider.dart';
import 'package:sleep/provider/wave_deatil_provider/pluse_detail_provider.dart';
import 'package:sleep/utils/player.dart';

enum FatigueState {
  normal,
  mild,
  moderate,
  severe,
}

class FatigueStateModel {
  final FatigueState state;
  final int time;

  FatigueStateModel(this.state, this.time);
}

class FatigueStateHelp {
  final FatigueSetting setting;

  FatigueStateHelp(this.setting);
  FatigueState currentState(double ct, double hrv, double perclos) {
    if (isSevere(ct, hrv, perclos)) return FatigueState.severe;
    if (isModerate(ct, hrv, perclos)) return FatigueState.moderate;
    if (isMild(ct, hrv, perclos)) return FatigueState.mild;
    return FatigueState.normal;
  }

  bool isMild(double ct, double hrv, double perclos) {
    return ct > setting.CTmild! ||
        perclos > setting.PERCLOSmild! ||
        hrv < setting.HRVmild!;
  }

  bool isModerate(double ct, double hrv, double perclos) {
    return ct > setting.CTmoderate! ||
        perclos > setting.PERCLOSmoderate! ||
        hrv < setting.HRVmoderate!;
  }

  bool isSevere(double ct, double hrv, double perclos) {
    return ct > setting.CTsevere! ||
        perclos > setting.PERCLOSsevere! ||
        hrv < setting.HRVsevere!;
  }
}

class FatigueStatePro {
  final FatigueState? state;
  final List<List<FlSpot>> fatigueSpot;

  FatigueStatePro(this.state, this.fatigueSpot);
}

int before = 0;
final fatigueStateProvider = Provider<FatigueStatePro?>((ref) {
  final fatigue = ref.watch(fatigueDetailProvider);
  final pluse = ref.watch(pulseDetailProvider);
  final setting = ref
      .watch(dbProvider)
      .fatuigeSetting()
      .conbine(FatigueSetting.defaultSetting());

  final ct = fatigue.wave?.ct;
  final hrv = pluse.hrv;

  final perclos = fatigue.wave?.perclose;
  final now = DateTime.now().millisecondsSinceEpoch;
  if (MerroyDB.share().fatigueStartTime == null) {
    MerroyDB.share().fatigueStartTime = now; //设置初始值
  }
  final start = MerroyDB.share().fatigueStartTime;

  final current = DateTime.now().millisecondsSinceEpoch;

  List models = MerroyDB.share().fatigueStates;
  List<FlSpot> spots = [];
  List<List<FlSpot>> fullSpots = [];
  FatigueStateModel? lastModel;
  if (models.isNotEmpty) {
    FatigueStateModel first = models.first;

    for (FatigueStateModel model in models) {
      final spot = FlSpot(
          (model.time - first.time).toDouble(), model.state.index.toDouble());
      if (lastModel == null || lastModel.state == model.state) {
        spots.add(spot);
      } else {
        fullSpots.add(spots);
        spots = [];
      }
      lastModel = model;
    }
    if (spots.isNotEmpty) fullSpots.add(spots);
  }

  //每一秒采集一次状态
  if (ct != null && perclos != null && hrv != '-') {
    final state =
        FatigueStateHelp(setting).currentState(ct, double.parse(hrv), perclos);
    if (current - before > 1000) {
      before = current;

      MerroyDB.share().fatigueStates.add(FatigueStateModel(
            state,
            (current - start!) ~/ 1000,
          ));
    }

    if (state == FatigueState.moderate) {
      AlertUtils.share().setFagtigueMusic(true);
      AlertUtils.share().setFagtigueSocker(false);
    } else if (state == FatigueState.severe) {
      AlertUtils.share().setFagtigueMusic(true);
      AlertUtils.share().setFagtigueSocker(true);
    } else {
      AlertUtils.share().setFagtigueMusic(false);
      AlertUtils.share().setFagtigueSocker(false);
    }

    return FatigueStatePro(state, fullSpots);
  }

  AlertUtils.share().setFagtigueMusic(false);
  AlertUtils.share().setFagtigueSocker(false);
  return FatigueStatePro(null, fullSpots);
});
